﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ProjetPlatformer.gameElements;

namespace ProjetPlatformer.phys
{
    class CollisionManager
    {
        public static bool controlableOnGround = false;

        #region Static Method used by entire game for calculating seperation amount when two rects collide
        public static Vector2 CalcualteMinimumTranslationDistance(GameElement ge1, GameElement ge2)
        {
            Vector2 result = Vector2.Zero;
            float difference = 0.0f;
            float minimumTranslationDistance = 0.0f;  //The absolute minimum distance we'll need to separate our colliding object.  
            int axis = 0; // Axis stores the value of X or Y.  X = 0, Y = 1.  
            int side = 0; // Side stores the value of left (-1) or right (+1).  

            // Left  
            difference = ge1.bBox.Right - ge2.bBox.Left;
            minimumTranslationDistance = difference;
            axis = 0;
            side = -1;

            // Right  
            difference = ge2.bBox.Right - ge1.bBox.Left;
            if (difference < minimumTranslationDistance)
            {
                minimumTranslationDistance = difference;
                axis = 0;
                side = 1;
            }

            // Down  
            difference = ge1.bBox.Bottom - ge2.bBox.Top;
            if (difference < minimumTranslationDistance)
            {
                minimumTranslationDistance = difference;
                axis = 1;
                side = -1;
                if (!controlableOnGround)
                {
                    CollisionManager.controlableOnGround = true;
                }
                //DEBUG si le heros est au sol
                //Console.WriteLine("onground actually");
            }

            // Up  
            difference = ge2.bBox.Bottom - ge1.bBox.Top;
            if (difference < minimumTranslationDistance)
            {
                minimumTranslationDistance = difference;
                axis = 1;
                side = 1;
            }

            if (axis == 1) //Y Axis  
            { result.Y = (float)side * minimumTranslationDistance; }
            else
            {   //X Axis  
                result.X = (float)side * minimumTranslationDistance;
            }

            return result;
        }
        #endregion 
    }
}
